package com.financial.sys.service.bid.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.financial.model.pojo.TBid;
import com.financial.model.pojo.TBorrow;
import com.financial.model.pojo.TUserWallet;
import com.financial.sys.mapper.bid.TBidMapper;
import com.financial.sys.mapper.borrow.TBorrowMapper;
import com.financial.sys.mapper.borrow.TUserWalletMapper;
import com.financial.sys.service.bid.TBidService;
import com.financial.sys.util.FinanceCalcUtils;
import com.financial.sys.vo.bid.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzy
 * @since 2020-02-12
 */
@Service("bidService")
public class TBidServiceImpl extends ServiceImpl<TBidMapper, TBid> implements TBidService {
	//1
	@Autowired
	private TBidMapper tBidMapper;
	@Autowired
	TBorrowMapper tBorrowMapper;
	@Autowired
	TUserWalletMapper tUserWalletMapper;


	@Override
	public Map<String, Object> getBidQuery(QueryObject queryObject) {
		Page<TBid> page = new Page<>(queryObject.getCurrentPage(), queryObject.getPageSize());
		Page<TBid> bidPage = page.setRecords(tBidMapper.getBidQuery(queryObject, page));
		HashMap<String, Object> map = new HashMap<>();
		map.put("currentPage", bidPage.getCurrent());
		map.put("pageSize", bidPage.getSize());
		//是否存在上一页
		if (bidPage.hasPrevious()) {
			map.put("prevPage", bidPage.getCurrent() - 1);
		} else {
			map.put("prevPage", bidPage.getCurrent());
		}

		//是否存在下一页
		if (bidPage.hasNext()) {
			map.put("nextPage", bidPage.getCurrent() + 1);
		} else {
			map.put("nextPage", bidPage.getCurrent());
		}
		map.put("totalCount", bidPage.getTotal());
		map.put("totalPage", bidPage.getPages());
		map.put("listData", bidPage.getRecords());
		return map;
	}

	@Override
	public List getByBorrowId(String borrowId) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("borrow_id", borrowId);
		return baseMapper.selectList(queryWrapper);
	}

	@Override
	public Integer addBid(TBid bid) {
		//获取borrow对象信息
		String borrowId = bid.getBorrowId();
		TBorrow tBorrow = tBorrowMapper.selectById(borrowId);

		bid.setYearRate(tBorrow.getYearRate());
		bid.setBorrowState(tBorrow.getBorrowState());
		//获取投标人id
		String bidUserId = bid.getBidUserId();
		//投标金额数
		long currentBidAmount = 0;
		//投标利息数
		long bidInterest = 0;
		//投标成功

		//通过投标用户id获取bid投标对象
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("bid_user_id", bidUserId);
		queryWrapper.eq("borrow_id", borrowId);
		TBid tBid = baseMapper.selectOne(queryWrapper);


		//如果对象为空,不同人投资
		if (StringUtils.isEmpty(tBid) || tBid == null) {
			currentBidAmount = bid.getBidAmount();
			//根据投标计算  投标利息
			bidInterest = FinanceCalcUtils.calcInterestPending(bid.getBidAmount(), tBorrow.getBorrowAmount(), tBorrow.getTotalInterest());
			bid.setBidInterest(bidInterest);
			baseMapper.insert(bid);
			//投标数量加1
			tBorrow.setBidNum(tBorrow.getBidNum() + 1);
			//如果不为空,则同一位投资人多次投资
		} else {
			currentBidAmount = tBid.getBidAmount() + bid.getBidAmount();
			tBid.setBidAmount(tBid.getBidAmount() + bid.getBidAmount());
			//根据投标计算  投标利息
			bidInterest = FinanceCalcUtils.calcInterestPending(tBid.getBidAmount(), tBorrow.getBorrowAmount(), tBorrow.getTotalInterest());
			tBid.setBidInterest(bidInterest);
			baseMapper.updateById(tBid);
		}

		//已投标金额等于借款金额
		if (currentBidAmount == tBorrow.getBorrowAmount()) {
			tBorrow.setBorrowState(30);
		}

		//当前已投标金额
		tBorrow.setCurrentBidAmount(tBorrow.getCurrentBidAmount() + bid.getBidAmount());
		//当前已投标利息
		tBorrow.setCurrentBidInterest(bidInterest);
		tBorrowMapper.updateById(tBorrow);


		//投资人可用金额减少,冻结金额增加
		Long bidAmount = bid.getBidAmount();
		TUserWallet tUserWallet = tUserWalletMapper.selectById(bidUserId);
		tUserWallet.setFreezeAmount(bidAmount);
		tUserWallet.setAvailableAmount(tUserWallet.getAvailableAmount() - bidAmount);
		tUserWalletMapper.updateById(tUserWallet);
		return 1;
	}
}
